#include "ProcessData.h"

u8 sendArr[SEND_DATA_SIZE] = {  0xAA,0x75,0x00,0x00,0x00,
								0x00,0x00,0x00,0x00,0x00,
								0x00,0x00,0x00,0x00,0x00,
								0x00,0x00,0x00,0x00,0x00 };

static void Hex_TO_BCD(u8 hexArr[]);
static u8 CRC_Check(u8 dataArr[],u8 arrLenth);

static u8 CRC_Check(u8 dataArr[],u8 arrLenth){
	u8 i,crcValue = 0;
	for(i = 0;i < arrLenth;i++){
		crcValue ^= dataArr[i];
	}
	return crcValue;
}

static void Hex_TO_BCD(u8 hexArr[]){
	u8 i = 0;
	do{
		hexArr[i] = (hexArr[i]/10*16)+(hexArr[i]%10);
	}while(hexArr[++i] !='\0');
}


void Send_Conmand(u8 sensorID[],u8 conmand){
	u8 i;
	u8 dateArr[6] = {0x11,0x09,0x07,0x22,0x20,0x30};
	
	sendArr[2] = conmand;
	sendArr[4] = 14;
	for(i = 0;i < 8;i++){
		sendArr[i+6] = sensorID[i];
	}
	for(i = 0;i < 6;i++){
		Hex_TO_BCD(dateArr);
		sendArr[i+14] = dateArr[i];	
	}
	sendArr[20] = CRC_Check(sendArr,20);
	
	GPIO_SetBits(GPIOG,GPIO_Pin_3);
	for(i = 0;i < SEND_DATA_SIZE;i++){
		USART_SendData(SENSORPOT, sendArr[i]);
		while(USART_GetFlagStatus(SENSORPOT,USART_FLAG_TXE)==RESET);
	}
	GPIO_ResetBits(GPIOG,GPIO_Pin_3);
}

